package com.zzxy.spring12.aspect;

import com.zzxy.spring12.service.ITeachaerService;
import com.zzxy.spring12.service.impl.TeacherServiceImpl;
import net.sf.cglib.proxy.Enhancer;
import net.sf.cglib.proxy.MethodInterceptor;
import net.sf.cglib.proxy.MethodProxy;

import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;

public class MyBeanFactoryCglib {
    public  static ITeachaerService createTeacherService(){
      ITeachaerService teacherService = new TeacherServiceImpl();

        MyAspect aspect = new MyAspect();
        Enhancer enhancer = new Enhancer();
       enhancer.setSuperclass(teacherService.getClass());
       enhancer.setCallback(new MethodInterceptor() {
           @Override
           public Object intercept(Object proxy, Method method, Object[] args, MethodProxy methodProxy) throws Throwable {
               aspect.before();
               Object obj = methodProxy.invokeSuper(proxy, args);
               aspect.after();
               return obj;

           }
       });
        TeacherServiceImpl proxt = (TeacherServiceImpl) enhancer.create();
        return null;

    }
}
